package www.dianbaozu.com.ui.login

import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentForgetPasswordBinding
import www.dianbaozu.com.ui.ProxyViewModel
import www.wen.com.common.lib.base.fragment.BaseReceiveSMSFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.extension.checkIsPassword
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.KeyHelper.FORGET_REMAINDER_TIME
import www.wen.com.common.lib.helper.loading.KeyHelper.FORGET_SYSTEM_REMAINDER_TIME
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.MD5Util
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description:忘记密码
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class ForgetPasswordFragment : BaseReceiveSMSFragment<FragmentForgetPasswordBinding>(), View.OnClickListener {

    /**
     * true 为忘记密码 false为重置密码
     */
    private var isForgetOrResetPassword: Boolean = false

    private val forgetPasswordViewModel: ForgetPasswordViewModel by viewModels()

    private val proxyViewModel: ProxyViewModel by activityViewModels()

    override val viewModel: BaseViewModel
        get() = forgetPasswordViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(requireActivity(), action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_forget_password

    override fun getRecordTimeKey(): String = FORGET_REMAINDER_TIME

    override fun getSystemRecordTimeKey(): String = FORGET_SYSTEM_REMAINDER_TIME

    override fun lazyInitView() {
        super.lazyInitView()
        isForgetOrResetPassword = requireArguments().getBoolean(BUNDLE_DATA, false)
        dataBinding.run {
            proxyViewModel = this@ForgetPasswordFragment.proxyViewModel
            forgetPasswordViewModel = this@ForgetPasswordFragment.forgetPasswordViewModel

            if(!isForgetOrResetPassword) {
                view.isVisible = false
                strWelcome.text =  getString(R.string.forgotPassword)
            }else{
                tvPhoneNumber.setText(SpUtil.decodeString(KeyHelper.MMKV_MOBILE))
                ivDropDown.isVisible = false
                view.isVisible = true
                tvCountryAreaCode.isVisible = false
                tvCountryAreaCode.isVisible = false
                strWelcome.text =  getString(R.string.reset_password)
            }

        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.slVerificationCode -> {
                sendVerificationCode(
                        dataBinding.tvPhoneNumber,
                        dataBinding.etVerificationCode,
                        proxyViewModel.itac.value!!,
                        forgetPasswordViewModel
                )
            }

            R.id.tvResetPassword -> {
                if(!forgetPasswordViewModel.mPassWord.value!!.checkIsPassword()) return

                forgetPasswordViewModel.requestResetPassword(isForgetOrResetPassword, RequestBodyParameter(
                        mobile = forgetPasswordViewModel.mPhone.value!!,
                        password = MD5Util.MD5(forgetPasswordViewModel.mPassWord.value!!),
                        smsCode = forgetPasswordViewModel.mVerificationCode.value!!,
                )) { actionLeft() }
            }

            R.id.tvCountryAreaCode -> {
                showAreaPopupView()
            }
        }
    }

    private fun showAreaPopupView() {
        CreateXPopupHelper.createBottomAreaCodeListPopupView(PopupViewEntity().apply {
            this.context = requireActivity()
            isShow = true
            itac = proxyViewModel.itac.value!!
            areaCodeList = proxyViewModel.areaCodeEntities.value
            areaCodeListResultCallBack = {
                "+${it.code}".also { area ->
                    dataBinding.tvCountryAreaCode.text = area
                }
                proxyViewModel.itac.value = it.code
                SpUtil.encode(KeyHelper.MMKV_ITAC, it.code)
            }
        })
    }


}



